﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace Formsy.Providers.DataSource
{
    public class XmlProvider : DataSourceProvider
    {
        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
        }


        /// <summary>
        /// Returns a list of items to be databound.
        /// </summary>
        /// <returns></returns>
        public override List<ListItem> GetListItems(List<XElement> parameters)
        {
            // get parameters from param
            string path = parameters[0].Value;
            string rootName = parameters[1].Value;
            string nodeName = parameters[2].Value;

            // load the xml
            XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(path));

            // Get items
            var root = doc.Descendants(rootName).FirstOrDefault();
            var genres = doc.Descendants(nodeName);

            // Fill the item list!
            List<ListItem> items = new List<ListItem>();
            foreach (var x in genres)
            {
                string s = x.Value.Trim();
                items.Add(new ListItem(s, s));
            }
            return items;

        }

    }
}
